<?php
namespace app\common\logic;

use app\common\exception\FailException;
use think\Controller;
use app\common\model\Order as OrderModel;

class Order extends Controller
{
    public function get_list($userid,$start_time="",$end_time="",$start=1,$num=20,$evaluate=0,$type=0){

        $where=[];
        if($start_time){
            $where['o.created_at']=['egt',$start_time];
        }

        if($end_time){
            $where['o.created_at']=['elt',$end_time];
        }

        if($start_time&&$end_time){
            $where['o.created_at']=['between',[$start_time,$end_time]];
        }

        if($evaluate){
            $where['o.evaluate']=['eq',$evaluate];
        }

        if($type){
            $where['o.type']=['eq',$type];
        }

          $where['o.status']=['eq',1];
        $data = OrderModel::alias('o')
            ->join('t_shop s','o.shop_id = s.id','LEFT')
            ->join('t_picture p','s.thumb = p.id','LEFT')
            ->where($where)
            ->where("o.user_id",$userid)
            ->page($start,$num)
            ->field("s.name,p.picurl,o.*")->order("o.created_at desc")->select();
        $count = OrderModel::alias('o')
            ->join('t_shop s','o.shop_id = s.id','LEFT')
            ->join('t_picture p','s.thumb = p.id','LEFT')
            ->where("o.user_id",$userid)
            ->field("s.name,p.picurl,o.*")->count();

        $sum = OrderModel::alias('o')
            ->join('t_shop s','o.shop_id = s.id','LEFT')
            ->join('t_picture p','s.thumb = p.id','LEFT')
            ->where("o.user_id",$userid)
            ->field("s.name,p.picurl,o.*")->sum('total_price');

        return [$data,$count,$sum];


    }


    public function get_shop_list($shopid,$start_time="",$end_time="",$start=1,$num=20,$evaluate=0,$type=0,$number=0){

        $where=[];
        if($start_time){
            $where['o.created_at']=['egt',$start_time];
        }

        if($end_time){
            $where['o.created_at']=['elt',$end_time];
        }

        if($start_time&&$end_time){
            $where['o.created_at']=['between',[$start_time,$end_time]];
        }

        if($evaluate){
            $where['o.evaluate']=['eq',$evaluate];
        }

        if($type){
            $where['o.type']=['eq',$type];
        }

        if($number){
            $where['o.sn']=['like','%'.$number.'%'];
        }
         $where['o.status']=['eq',1];


        $data = OrderModel::alias('o')
            ->join('t_shop s','o.shop_id = s.id','LEFT')
            ->join('t_picture p','s.thumb = p.id','LEFT')
            ->where($where)
            ->where("o.shop_id",$shopid)
            ->page($start,$num)
            ->field("s.name,p.picurl,o.*")->order("o.created_at desc")->select();
        $count = OrderModel::alias('o')
            ->join('t_shop s','o.shop_id = s.id','LEFT')
            ->join('t_picture p','s.thumb = p.id','LEFT')
            ->where("o.shop_id",$shopid)
            ->field("s.name,p.picurl,o.*")->count();

        $sum = OrderModel::alias('o')
            ->join('t_shop s','o.shop_id = s.id','LEFT')
            ->join('t_picture p','s.thumb = p.id','LEFT')
            ->where("o.shop_id",$shopid)
            ->field("s.name,p.picurl,o.*")->sum('total_price');

        return [$data,$count,$sum];


    }

    public function get_info($userid,$id){

        $where=[];
        if($id){
            $where['o.id']=['eq',$id];
        }


        $data = OrderModel::alias('o')
            ->join('t_shop s','o.shop_id = s.id','LEFT')
            ->join('t_picture p','s.thumb = p.id','LEFT')
            ->where($where)
            ->where("o.user_id",$userid)
            ->field("s.name,p.picurl,o.*")->find();
        if(!$data){
            throw new FailException(['code' => 201,"msg"=>"非法操作,数据流失"]);
        }

        return $data;


    }
}